<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/11/19
 * Time: 15:37
 */

namespace app\index\table;

use app\index\controller\Login;
use think\Db;
use app\index\service\ExcelService;

class FacilityAlterTable extends BaseTable
{
    protected static $view = 'V_ASSET_EQUIP_ALTERINFO';
    protected static $table = 'ASSET_EQUIP_ALTERINFO';
    protected static $pk = 'RECEIPT_NO';

    static public function table(){
        return Db::table(static::$view);
    }

    static public function add($data){
        $data['OPERATOR'] = Login::getUserId();
        $data['INPUTDATE'] = date('Y-m-d');

        self::toDate($data,'ALTER_DATE');
        self::toDate($data,'INPUTDATE');
        Db::table(static::$table)->insert($data);
    }

    static public function delete($where){
        return Db::table(static::$table)->where($where)->delete();
    }

    public static function getNextId(){
        $year = date('Y');
        $numMaxRecord = Db::table(static::$table)
            ->field('max(SUBSTR('.static::$pk.',5,6)) as NUM')
            ->where([['SUBSTR('.static::$pk.',1,4)','=',$year]])
            ->fetchSql(true)
            ->find();
        $numMaxRecord = Db::query($numMaxRecord);
        $num = $numMaxRecord[0]['NUM'];
        if ($num == NULL) {
            $num = 0;
        }
        $num++;
        return $year.sprintf("%06d", $num);
    }

    public static function getReceiptNo(){
        $year = date('Y');
        $numMaxRecord = Db::table(static::$table)
            ->field('max(SUBSTR(RECEIPT_NO,5,6)) as NUM')
            ->where([['SUBSTR(RECEIPT_NO,1,4)','=',$year]])
            ->fetchSql(true)
            ->find();
        $numMaxRecord = Db::query($numMaxRecord);
        $num = $numMaxRecord[0]['NUM'];
        if ($num == NULL) {
            $num = 0;
        }
        $num++;
        return $year.sprintf("%06d", $num);
    }

    public static function getDisposeNo(){
        $year = date('Y');
        $numMaxRecord = Db::table(static::$table)
            ->field('max(SUBSTR(DISPOSE_RECNO,5,6)) as NUM')
            ->where([['SUBSTR(DISPOSE_RECNO,1,4)','=',$year]])
            ->fetchSql(true)
            ->find();
        $numMaxRecord = Db::query($numMaxRecord);
        $num = $numMaxRecord[0]['NUM'];
        if ($num == NULL) {
            $num = 0;
        }
        $num++;
        return $year.sprintf("%06d", $num);
    }

    public static function exportExcel($data,$fileName){

        $fieldList = [
            ['title'=>'学院','field'=>'COLLEGE'],
            ['title'=>'年份','field'=>'YEAR'],
            ['title'=>'资产编号','field'=>'LIQUID_NO'],
            ['title'=>'资产名称','field'=>'LIQUID_NAME'],
            ['title'=>'资产类型','field'=>'LIQUID_TYPE'],
            ['title'=>'价值','field'=>'LIQUID_FEE'],
            ['title'=>'备注','field'=>'REMARK'],
            ['title'=>'制单人','field'=>'INPUT_NAME'],
            ['title'=>'制单时间','field'=>'INPUT_TIME'],
        ];

        $excel = new ExcelService();
        $excel->exportExcel($data,$fileName,$fieldList);
    }




}